<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Quizzes_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('quiz_model', '', TRUE);
        $this->load->model('group_model', '', TRUE);
        if (!$this->session->userdata('logged_in')) {
            redirect('login');
        }

        $this->load->library('authentication');$this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();$this->authentication->redirectIfNotBe(get_employee_titles());
    }

    function index($limit = '0') {
        show_404();
        $logged_in = $this->session->userdata('logged_in');
        //if ($logged_in['su'] == "1") {
        //$data['result'] = $this->quiz_model->quiz_list($limit);
        //} else {
        $gid = $logged_in['gid'];
        $data['result'] = $this->quiz_model->quiz_list($limit, $gid);
        //}
        $data['title'] = "Quiz/Test";
        //$data['limit'] = $limit;

        $this->load->view(get_folder($this->loggedUser->title).'/quizzes_list', $data);
    }

    function add_new() {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }

        $this->load->model('category', '', TRUE);
        $data['category'] = $this->category->category_dropdown();
        $this->load->model('difficult_level', '', TRUE);
        $data['groups'] = $this->group_model->get_allgroups();

        $data['difficult_level'] = $this->difficult_level->level_dropdown();
        if ($this->input->post('submit_quiz')) {
            $data['resultstatus'] = $this->quiz_model->add_quiz();
        }
        $data['title'] = "Add new quiz";
        $this->load->view('header', $data);

        $this->load->view('new_quiz', $data);
        $this->load->view('footer', $data);
    }

    function edit_quiz($id) {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }

        $this->load->model('category', '', TRUE);
        $data['category'] = $this->category->category_dropdown();
        $this->load->model('difficult_level', '', TRUE);
        $data['groups'] = $this->group_model->get_allgroups();

        $data['difficult_level'] = $this->difficult_level->level_dropdown();
        if ($this->input->post('submit_quiz')) {
            $data['resultstatus'] = $this->quiz_model->edit_quiz($id);
        }
        $data['result'] = $this->quiz_model->quiz_detail($id);
        $data['assigned_gids'] = $this->quiz_model->assigned_groups($id);
        $data['assigned_questions'] = $this->quiz_model->assigned_questions($id);

        $data['title'] = "Edit quiz";
        $this->load->view('header', $data);

        $this->load->view('edit_quiz', $data);
        $this->load->view('footer', $data);
    }

    function remove_quiz($id) {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }

        $data['resultstatus'] = $this->quiz_model->remove_quiz($id);
        redirect('quiz', 'refresh');
    }

    function attempt($id) {
        $quiz = $this->quiz_model->quiz_detail($id);
        
        if ($quiz != false) {
            $viewData['result'] = $quiz;
            $viewData['title'] = $viewData['result']->quiz_name;
            $this->load->view(get_folder($this->loggedUser->title).'/quiz_before_attempt', $viewData);
        }
        else{
            show_404();
            //$this->load->view('user/no_quiz');
        }
    }

    // update individual question time spent
    function update_time($id, $qtime) {
        $this->quiz_model->update_time($id, $qtime);
    }

    // update answer
    function update_answer($id, $oid) {
        $this->quiz_model->update_answer($id, $oid);
    }

    function access_test($id) {
        $this->load->helper('cookie');
        $this->load->model('user');
        $user = $this->user->get_user_by_code($this->loggedUser->code);
        if(strtotime($user['starting_day']) >= strtotime(date('Y-5-1'))) // bắt đầu làm việc sau ngày 1/5
        {
            $viewData['error'] = true;
            $viewData['message'] = "Bắt đầu làm việc sau ngày 1/5 không cần làm bài trắc nghiệm này.";
            $this->load->view(get_folder($this->loggedUser->title).'/quiz_before_attempt', $viewData);
        }
        if($user['status'] == 'pregnant')
        {
            $viewData['error'] = true;
            $viewData['message'] = "Bạn đang trong kì nghỉ, không cần phải làm bài trắc nghiệm này.";
            $this->load->view(get_folder($this->loggedUser->title).'/quiz_before_attempt', $viewData);
        }
        if($user['status'] == 'inactive')
        {
            $viewData['error'] = true;
            $viewData['message'] = "Tài khoản đang bị ngưng hoạt động.";
            $this->load->view(get_folder($this->loggedUser->title).'/quiz_before_attempt', $viewData);
        }

        $viewData['error'] = false;
        $viewData['message'] = $this->quiz_model->quiz_verify($id);
        $viewData['quiz_id'] = $id;
        $viewData['result'] = $this->quiz_model->quiz_detail($id);
        $viewData['title'] = $viewData['result']->quiz_name;
        if ($viewData['message'] == "1") {
            if (!$this->input->cookie('rid', TRUE)) { 
                redirect('user/quizzes/access_test/' . $id, 'refresh');
            }
            $rid = $this->input->cookie('rid', TRUE);

            // get assignied questions
            $viewData['assigned_question'] = $this->quiz_model->get_question($rid);
            // get time information
            $viewData['time_info'] = $this->quiz_model->get_time_info($rid);
            // time remaining in seconds
            // total time - time spent
            $viewData['seconds'] = (($viewData['result']->duration) * 60) - ($viewData['time_info']['time_spent']);

            // get quiz data like quiz duration, title
            $viewData['quiz_data'] = $this->quiz_model->get_quiz_data($id);

            // load quiz access page
            $this->load->view(get_folder($this->loggedUser->title).'/quiz_access', $viewData);
        } else {
            $viewData['error'] = true;
            
            $this->load->view(get_folder($this->loggedUser->title).'/quiz_before_attempt', $viewData);
        }
    }

    function submit_quiz($id) {

        $this->load->helper('cookie');

        $viewData['resultstatus'] = $this->quiz_model->quiz_submit($id);
        $viewData['result'] = $this->quiz_model->quiz_detail($id);
        $viewData['title'] = $viewData['result']->quiz_name;


//        $this->load->view('header', $viewData);
//        $this->load->view('quiz_detail', $viewData);
//        $this->load->view('footer', $viewData);

        $rid = $this->input->cookie('rid', TRUE);

        redirect('user/results/view/' . $rid . '/' . $id);
    }

}

?>
